//****************************************************************
//* Copyright (c) 2008 Ford Motor Company. All Rights Reserved.
//*
//* $$Workfile:   EconomicIndexStatusAction.java  $$
//* $$Revision:   1.2  $$
//* $$Author:   TMALDON4  $$
//* $$Date:   Feb 06 2009 05:32:06  $$
//*
//*****************************************************************
package ar.com.ford.it.fullEconomics.view.action;

import java.util.ArrayList;
import java.util.Collection;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import ar.com.ford.it.fullEconomics.exception.FullEconomicsException;
import ar.com.ford.it.fullEconomics.facade.EconomicIndexDefFacade;
import ar.com.ford.it.fullEconomics.facade.EconomicSourceFacade;
import ar.com.ford.it.fullEconomics.struct.OptionCombo;
import ar.com.ford.it.fullEconomics.view.form.EconomicIndexStatusForm;
import ar.com.ford.it.fullEconomics.wslx.FullEconomicsWslxUser;
import ar.com.ford.it.webengine.facade.FacadeProvider;
import ar.com.ford.it.webengine.struts.AbstractBaseAction;
import ar.com.ford.it.webengine.struts.FordActionMapping;
import ar.com.ford.it.webengine.websession.WebSession;

public class EconomicIndexStatusAction extends AbstractBaseAction {

    /**
     * 
     */
    public ActionForward initPage(FordActionMapping mapping,
                                  ActionForm actionForm,
                                  HttpServletRequest request,
                                  HttpServletResponse response,
                                  WebSession webSession, ActionMessages msgs,
                                  ActionMessages warns, ActionMessages errorMsgs) {
        EconomicIndexStatusForm form = (EconomicIndexStatusForm) actionForm;
        EconomicSourceFacade facade = (EconomicSourceFacade) FacadeProvider
                .getFacade(request, EconomicSourceFacade.class);
        FullEconomicsWslxUser user = (FullEconomicsWslxUser) webSession
                .getUser();

        try {
            form.setEconomicSources(facade.findEconomicSources(null, null, user
                    .getTeesg01Plant()));
            request.getSession().setAttribute("economicSources",
                    form.getEconomicSources());
//          Combo tpype Economics
            Collection<OptionCombo> ecoType = new ArrayList<OptionCombo>();
            OptionCombo actual = new OptionCombo("A", "Actual");
            ecoType.add(actual);
            OptionCombo forcasted = new OptionCombo("F", "Futuro");
            if (user.getPermissions().contains("PERM_F")) {
                ecoType.add(forcasted);
            }
            request.getSession().setAttribute("economicsTypes", ecoType);
            form.resetForm();
        } catch (FullEconomicsException e) {
            ActionMessages am = new ActionMessages();
            am.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(e
                    .getMessageResource()));
            saveErrors(request, am);
        }

        return mapping.findForward("success");
    }

    /**
     * 
     * @param mapping
     * @param actionForm
     * @param request
     * @param response
     * @param webSession
     * @param msgs
     * @param warns
     * @param errorMsgs
     * @return
     */
    public ActionForward findEconomicIndexStatus(FordActionMapping mapping,
                                                 ActionForm actionForm,
                                                 HttpServletRequest request,
                                                 HttpServletResponse response,
                                                 WebSession webSession,
                                                 ActionMessages msgs,
                                                 ActionMessages warns,
                                                 ActionMessages errorMsgs) {
        EconomicIndexStatusForm form = (EconomicIndexStatusForm) actionForm;
        EconomicIndexDefFacade facade = (EconomicIndexDefFacade) FacadeProvider
                .getFacade(request, EconomicIndexDefFacade.class);
        FullEconomicsWslxUser user = (FullEconomicsWslxUser) webSession
                .getUser();

        try {
            form.setUpdateStatusCol(facade.getEconomicSourceStatus(user
                    .getTeesg01Plant(), form.getYear(), form
                    .getEconomicSourceSel(), form.getEconValueTypeSelected()));
        } catch (FullEconomicsException e) {
            ActionMessages am = new ActionMessages();
            am.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(e
                    .getMessageResource()));
            saveErrors(request, am);
        }

        return mapping.findForward("success");
    }

    /**
     * 
     * @param mapping
     * @param actionForm
     * @param request
     * @param response
     * @param webSession
     * @param msgs
     * @param warns
     * @param errorMsgs
     * @return
     */
    public ActionForward cleanEconomicIndexStatus(FordActionMapping mapping,
                                                  ActionForm actionForm,
                                                  HttpServletRequest request,
                                                  HttpServletResponse response,
                                                  WebSession webSession,
                                                  ActionMessages msgs,
                                                  ActionMessages warns,
                                                  ActionMessages errorMsgs) {
        EconomicIndexStatusForm form = (EconomicIndexStatusForm) actionForm;

        form.cleanFindFields();
        return mapping.findForward("success");
    }

    /**
     * Setea en la request los parametros que va a necesitar el EconomicIndexValuesAction,
     * que son la fecha y el Economic Source que selecciono el usuario de la grilla de
     * Economic Sources Status
     * 
     * @param mapping
     * @param actionForm
     * @param request
     * @param response
     * @param webSession
     * @param msgs
     * @param warns
     * @param errorMsgs
     * @return
     */
    public ActionForward findEconomicIndexValues(FordActionMapping mapping,
                                                 ActionForm actionForm,
                                                 HttpServletRequest request,
                                                 HttpServletResponse response,
                                                 WebSession webSession,
                                                 ActionMessages msgs,
                                                 ActionMessages warns,
                                                 ActionMessages errorMsgs) {
        EconomicIndexStatusForm form = (EconomicIndexStatusForm) actionForm;
        String dateSelected = null;
        String month = null;
        FullEconomicsWslxUser user = (FullEconomicsWslxUser) webSession
        .getUser();        
        
        if (Integer.parseInt(form.getMonthSelected()) < 10) {
            month = "0" + form.getMonthSelected();
        } else {
            month = form.getMonthSelected();
        }
        
        dateSelected = "01/" + month + "/" + form.getYear();
        request.setAttribute("econSourceStatusDate", dateSelected);        
        request.setAttribute("econSourceStatusNumber", form.getEconSourceNumberSelected());
        if (form.getEconValueTypeSelected().equals("F")) {
            if (!user.getPermissions().contains("PERM_F")) {
                setNoPermissionsError(request);
            } else {
                request.setAttribute("econTypeEconomic", form.getEconValueTypeSelected());
            }
        } else {
            request.setAttribute("econTypeEconomic", form.getEconValueTypeSelected());
        }
        return mapping.findForward("success");
    }

    private void setNoPermissionsError (HttpServletRequest request) {
        ActionMessages am = new ActionMessages();
        am.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("economicValue.error.permForcas"));
        saveErrors(request, am);        
    }
    
}
